package top.z.orm.handle.osql.chain;

import top.z.orm.core.osql.dialect.DbFeature;
import top.z.orm.handle.osql.core.BeeFactory;

public class AbstractSelectToSql extends AbstractToSql {

    private DbFeature dbFeature = BeeFactory.getHoneyFactory().getDbFeature();

    protected int start;
    protected int size;

    public String toSQL() {
        // Oracle 用 JDBC 不允许有分号
        return toSQL(true);
    }

    public String toSQL(boolean noSemicolon) {
        if (noSemicolon) {
            return addPage(sql.toString());
        } else {
            return addPage(sql.toString()) + ";";
        }
    }

    private String addPage(String sql) {
        if (this.start != 0 && size != 0) {
            sql = dbFeature.toPageSql(sql, start, size);
        } else if (size != 0) {
            sql = dbFeature.toPageSql(sql.toString(), size);
        }
        return sql;
    }
}